//
//  YDTabBarController.m
//  Dental
//
//  Created by yidong on 2017/9/29.
//  Copyright © 2017年 东哥. All rights reserved.
//

#import "YDTabBarController.h"
#import "HomeViewController.h"
#import "SquareViewController.h"
#import "MakeMoneyViewController.h"
#import "MineViewController.h"

@interface YDTabBarController ()

@end

@implementation YDTabBarController

-(void)viewDidLoad
{
    [super viewDidLoad];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(selectClinic) name:@"selectClinic" object:nil];
    
    //首页
    HomeViewController *home =[[HomeViewController alloc] init];
    //UINavigationController  *homenavi = [[UINavigationController alloc] initWithRootViewController:homeVC];
    
    UITabBarItem *tabBar1 = [[UITabBarItem alloc] initWithTitle:@"首页" image:[UIImage imageNamed:@"首页灰色"] selectedImage:[UIImage imageNamed:@"首页点亮"]];
    
    tabBar1 .selectedImage = [[UIImage imageNamed:@"首页点亮"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    
    home.tabBarItem = tabBar1;
    
   //广场
   SquareViewController *square = [[SquareViewController alloc] init];
    
   //UINavigationController  *naviNear = [[UINavigationController alloc] initWithRootViewController:nearVC];
   
    UITabBarItem *tabBar2 = [[UITabBarItem alloc] initWithTitle:@"广场" image:[UIImage imageNamed:@"服务未点亮"] selectedImage:[UIImage imageNamed:@"服务点亮"]];
    
    tabBar2.selectedImage = [[UIImage imageNamed:@"服务点亮"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    square.tabBarItem = tabBar2;
    
    //赚钱
//    UINavigationController *navIM =[[UINavigationController alloc ] initWithRootViewController:[[MakeMoneyViewController alloc] init]];
    
   MakeMoneyViewController *money = [[MakeMoneyViewController alloc] init];
    UITabBarItem *tabBar3 = [[UITabBarItem alloc] initWithTitle:@"赚钱" image:[UIImage imageNamed:@"交流灰色"] selectedImage:[UIImage imageNamed:@"交流点亮"]];
    tabBar3.selectedImage = [[UIImage imageNamed:@"交流点亮"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    money.tabBarItem = tabBar3;
    
    //我的
    MineViewController *mine =[[MineViewController alloc] init];
    
    UITabBarItem *tabBarme = [[UITabBarItem alloc] initWithTitle:@"我的" image:[UIImage imageNamed:@"我的按下"] selectedImage:[UIImage imageNamed:@"我的按下"]];
    
    tabBarme.selectedImage = [[UIImage imageNamed:@"我的按下"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    mine.tabBarItem = tabBarme;

    self.viewControllers = @[home,square,money,mine];
    
    [self.viewControllers enumerateObjectsUsingBlock:^(__kindof UIViewController * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
       
        UIViewController *vc = (UIViewController *)obj;
        
        [self setTabBarItemfontAndColor:vc.tabBarItem];
        
    }];
    
}

#pragma mark - 设置tabBarItem的字体和颜色

-(void)setTabBarItemfontAndColor:(UITabBarItem *)tabBarItem{
    
    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor lightGrayColor],NSForegroundColorAttributeName, [UIFont systemFontOfSize:12],NSFontAttributeName,nil];
    
    [tabBarItem setTitleTextAttributes:dict forState:UIControlStateNormal];
    
    NSDictionary *dict2 = [NSDictionary dictionaryWithObjectsAndKeys:COLOR,
                           NSForegroundColorAttributeName,[UIFont systemFontOfSize:12],NSFontAttributeName,nil];
    
    [tabBarItem setTitleTextAttributes:dict2 forState:UIControlStateSelected];
    
}

-(void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
#pragma mark - 首页上点击我要找诊所按钮
-(void)selectClinic
{
    
    [self setSelectedIndex:1];
    [self animationWithIndex:1];

    
}

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
    
    NSInteger index = [self.tabBar.items indexOfObject:item];
    
    if (self.indexFlag != index) {
        [self animationWithIndex:index];
    }
    
}
// 动画
- (void)animationWithIndex:(NSInteger) index {
    
    NSMutableArray * tabbarbuttonArray = [NSMutableArray array];
    for (UIView *tabBarButton in self.tabBar.subviews) {
        
        if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            [tabbarbuttonArray addObject:tabBarButton];
        }
    }
    
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
    
    animation.values = @[@1.0,@1.3,@0.9,@1.15,@0.95,@1.02,@1.0];
    
    animation.duration = 0.88888888;
    
    animation.calculationMode = kCAAnimationCubic;
    
    // 把动画添加到对应控件的layer上去就OK了
    
    [[tabbarbuttonArray[index] layer]
     addAnimation:animation forKey:nil];
    
    self.indexFlag = index;

}
@end
